package com.example.androidexample.recyclerview;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidexample.R;
import com.example.basic.base.BaseActivity;

import java.util.ArrayList;

public class RecyclerViewActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RecyclerView rv;
    private SuperRvAdapter superRvAdapter;
    private RadioGroup rg_orientation;
    private MyDividerItemDecoration myDividerItemDecoration;

    private ArrayList<String> initData() {
        ArrayList<String> arrayList = new ArrayList<>(100);
        for (int i = 0; i < 100; i++) {
            arrayList.add("我是个文本" + i);
        }
        return arrayList;
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        rv = findViewById(R.id.rv);

        superRvAdapter = new SuperRvAdapter(this);
        superRvAdapter.setLayoutStyle(0,rv);
        superRvAdapter.setData(initData());


        ItemTouchHelper.Callback callback = new DragItemTouchHelper(superRvAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(rv);

        myDividerItemDecoration = new MyDividerItemDecoration(this, MyDividerItemDecoration.VERTICAL_LIST);
        rv.addItemDecoration(myDividerItemDecoration);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setAdapter(superRvAdapter);

        rg_orientation = findViewById(R.id.rg_orientation);
        rg_orientation.setOnCheckedChangeListener(this);
    }

    @Override
    protected int setLayoutXmlid() {
        return R.layout.activity_recyclerview;
    }

    @Override
    public String getTag() {
        return null;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int mStyle = 0;
        switch (checkedId) {
            case R.id.rb_0:
                mStyle = 0;
                break;
            case R.id.rb_1:
                mStyle = 1;
                break;
            case R.id.rb_2:
                mStyle = 2;
                break;
            case R.id.rb_3:
                mStyle = 3;
                break;
            case R.id.rb_4:
                mStyle = 4;
                break;
        }
        superRvAdapter.updateLayoutStyle(mStyle);
    }
}
